package repeat.personal.page;


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.List;

/**
 * 分页返回结果
 * @author Zhanggq
 * @date Aug 19, 2018
 */
@ApiModel("分页返回结果")
public class PageResult<T> {
	/**
	 * 当前页码
	 */
	@ApiModelProperty(value = "当前页码")
	private int pageNum;
	/**
	 * 每页数量
	 */
	@ApiModelProperty(value = "每页数量")
	private int pageSize;
	/**
	 * 记录总数
	 */
	@ApiModelProperty(value = "记录总数")
	private long totalSize;
	/**
	 * 页码总数
	 */
	@ApiModelProperty(value = "页码总数")
	private int totalPages;
	/**
	 * 分页数据
	 */
	@ApiModelProperty(value = "分页数据")
	private List<T> content;

	public PageResult() {
	}

	/**
	 * 构造分页结果
	 * @param pageNum 当前页码
	 * @param pageSize 每页数量
	 * @param totalSize 记录总数
	 * @param content 分页数据
	 */
	public PageResult(int pageNum, int pageSize, long totalSize, List<T> content) {
		this.pageNum = pageNum <= 0 ? 1 : pageNum;
		this.pageSize = pageSize == 0 ? 10 : pageSize;
		this.totalSize = totalSize;
		this.content = content;

		// 计算本次查询的总页数(总记录数/每页显示的记录数),如果能够整除,则页数为整除的结果;否则,需要在运算结果上加1.
		boolean isDivideExactly = (this.totalSize % this.pageSize == 0) ? true : false;
		this.totalPages = (int) (isDivideExactly ? (this.totalSize / this.pageSize)
				: (this.totalSize / this.pageSize) + 1);
	}

	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public long getTotalSize() {
		return totalSize;
	}
	public void setTotalSize(long totalSize) {
		this.totalSize = totalSize;
	}
	public int getTotalPages() {
		return totalPages;
	}
	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	public List<T> getContent() {
		return content;
	}
	public void setContent(List<T> content) {
		this.content = content;
	}
}
